package com.cicadamob.contact.api.group.http;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import com.cicadamob.contact.api.service.Contact;

public class JsonParseUtil {
	/**
	 * 将json转换成联系人
	 * @param json
	 * @param issucc  是解析成功的 还是解析失败的列表
	 * @return
	 */
	public static List<Contact> jsonTOcontact(String json,boolean issucc) {
		List<Contact> cfcontacts = new ArrayList<Contact>();
		try {
			JSONObject obj = new JSONObject(json);
			org.json.JSONArray array=null;
			if(!issucc){
				array = obj.optJSONArray("faillist");
				for (int i = 0; i < array.length(); i++) {
					Contact contact = new Contact();
					JSONObject item = array.optJSONObject(i);
					contact.setLinkPhone(item.optString("phone"));
					contact.setDisplayName(item.optString("displayname"));
					cfcontacts.add(contact);
				}
			}else{
				array = obj.optJSONArray("successlist");
				for (int i = 0; i < array.length(); i++) {
					Contact contact = new Contact();
					JSONObject item = array.optJSONObject(i);
					contact.setIdInGroup(item.optString("memberid"));
					contact.setLinkPhone(item.optString("phone"));
					contact.setDisplayName(item.optString("displayname"));
					contact.setFirstPy(item.optString("spname"));
					contact.setAllPy(item.optString("qpname"));
					contact.setFnamefpynum(item.optInt("fnameFpyNum"));
					cfcontacts.add(contact);
				}
			}

			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return cfcontacts;
	}
	
	/**
	 * 将成员转换成json
	 * 
	 * @param contacts
	 * @return
	 */
	public static String contactsIdsTOjson(List<Contact> contacts) {
		String contactStr = "";
		if (contacts != null && contacts.size() > 0) {
			try {
				JSONObject obj = new JSONObject();
				org.json.JSONArray array = new org.json.JSONArray();
				for (Contact contact : contacts) {
					array.put(contact.getIdInGroup());
				}
				obj.put("memberlist", array);
				contactStr = obj.toString();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return contactStr;
	}
	/**
	 * 将json转换成群组sql列表
	 * @param json
	 * @return
	 */
	public static List<String> jsonTOStringGroupList(String json){
		List<String> list=new ArrayList<String>();
		try {
			JSONObject obj = new JSONObject(json);
			org.json.JSONArray array = obj.optJSONArray("groupsql");
			for (int i = 0; i < array.length(); i++) {
				list.add(array.optString(i));
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
		
	}
	/**
	 * 将json转换成成员sql列表
	 * @param json
	 * @return
	 */
	public static List<String> jsonTOStringMemberList(String json){
		List<String> list=new ArrayList<String>();
		try {
			JSONObject obj = new JSONObject(json);
			org.json.JSONArray array = obj.optJSONArray("membersql");
			for (int i = 0; i < array.length(); i++) {
				list.add(array.optString(i));
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
		
	}
	
	
	/**
	 * 将成员转换成json
	 * 
	 * @param contacts
	 * @return
	 */
	public static String contactsTOjson(List<Contact> contacts) {
		String contactStr = "";
		if (contacts != null && contacts.size() > 0) {
			try {
				JSONObject obj = new JSONObject();
				org.json.JSONArray array = new org.json.JSONArray();
				for (Contact contact : contacts) {
					JSONObject item = new JSONObject();
					item.put("phone", contact.getLinkPhone()==null ? "":contact.getLinkPhone());
					item.put("displayname", contact.getDisplayName()==null? "":contact.getDisplayName());
					item.put("company", contact.getCompanyPath()==null ? "":contact.getCompanyPath());
					item.put("department", contact.getDepartment()==null ? "":contact.getDepartment());
					item.put("jobprofession", contact.getJobprofession()==null  ? "":contact.getJobprofession());
					item.put("sms", "1");
					array.put(item);
				}
				obj.put("memberlist", array);
				contactStr = obj.toString();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return contactStr;
	}
	
	
	/**
	 * 将json转换成contact(包含id) 模型
	 * 
	 * @param json
	 * @return
	 */
	public static List<Contact> jsonTOcontacthasId(String json) {
		List<Contact> cfcontacts = new ArrayList<Contact>();
		try {
			JSONObject obj = new JSONObject(json);
			org.json.JSONArray array = obj.optJSONArray("sucgroupmembers");
			for (int i = 0; i < array.length(); i++) {
				Contact contact = new Contact();
				JSONObject item = array.optJSONObject(i);
				contact.setLinkPhone(item.optString("phone"));
				contact.setDisplayName(item.optString("displayname"));
				contact.setIdInGroup(item.optString("id")); 
				cfcontacts.add(contact);
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return cfcontacts;
	}

}
